package link;


import java.util.Stack;

/**
 * @Program: dataOJ->SingleLinkList
 * @Description: 单链表
 * @Author: Tansty
 * @Date: 2022-01-18 20:17
 * @Version： 1.0
 **/

public class SingleLinkList {
    private Animal head = new Animal(0, "", "");

    public void add(Animal animal) {
        Animal temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = animal;
    }

    public void update(Animal animal) {
        Animal temp = head;
        boolean flag = false;
        while (temp.next != null) {
            temp = temp.next;
            if (temp.id == animal.id) {
                flag = true;
                break;
            }
        }
        if (flag) {
            temp.name = animal.name;
            temp.nickName = animal.nickName;
            System.out.println(temp);
        } else {
            System.out.println("没有您所修改的动物");
        }

    }

    public void list() {
        if (isNull()) {
            System.out.println("链表中没有内容");
            return;
        }
        Animal temp = head;
        while (temp.next != null) {
            temp = temp.next;
            System.out.println(temp);
        }
    }

    public boolean isNull() {
        return head.next == null;
    }

    public void delete(Animal animal) {
        Animal temp = head;
        boolean flag = false;
        while (temp.next != null) {
            if (temp.next.id == animal.id) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            if (temp.next.next == null) {
                temp.next = null;
            } else {
                temp.next = temp.next.next;
                temp.next.next = null;
            }

        } else {
            System.out.println("您所删除的动物不存在");
        }

    }

    public void addByOrder(Animal animal) {
        Animal temp = head;
        while (temp.next != null) {
            if (temp.next.id > animal.id) {
                animal.next = temp.next;
                break;
            }
            temp = temp.next;
        }
        temp.next = animal;
    }

    public Animal search(Animal animal) {
        if (isNull()) {
            System.out.println("链表中没有内容");
            return null;
        }
        Animal temp = head;
        while (temp.next != null) {
            temp = temp.next;
            if (temp.id == animal.id) {
                return temp;
            }
        }
        System.out.println("没有找到");
        return null;
    }

    public int getAnimalLen() {
        if (isNull()) {
            System.out.println("链表中没有内容");
            return 0;
        }
        int len = 0;
        Animal temp = head;
        while (temp.next != null) {
            temp = temp.next;
            len++;
        }
        return len;
    }

    public Animal getReverseAnimal(int k) {
        int len = getAnimalLen();
        if (k < 1 || k > len) {
            System.out.println("输入不合法");
            return null;
        }
        return search(new Animal(len - k + 1, "", ""));
    }

    public void reverseLink() {
        if (head.next == null || head.next.next == null) {
            return;
        }
        Animal temp = null;
        Animal cur = head.next;
        Animal other = new Animal(0, "", "");
        while (cur != null) {
            temp = cur.next;
            cur.next = other.next;
            other.next = cur;
            cur = temp;
        }
        head = other;
    }

    public void printReverseLink() {
        Stack<Animal> stack = new Stack<Animal>();
        Animal temp = head;
        while (temp.next != null) {
            stack.push(temp.next);
            temp = temp.next;
        }
        while (!stack.empty()) {
            System.out.println(stack.pop());
        }
    }

    public void combineOrderLink(SingleLinkList another) {
        Animal temp = head;
        Animal next = another.head;
        Animal other = new Animal(0, "", "");
        Animal t = other;
        while (temp.next != null && next.next != null) {
            if (temp.next.id <= next.next.id) {
                t.next = temp.next;
                temp = temp.next;
            } else {
                t.next = next.next;
                next = next.next;
            }
            t = t.next;
        }
        if (temp.next != null) {
            t.next = temp.next;
        }
        if (next.next != null) {
            t.next = next.next;
        }
        head = other;
    }

    public static void main(String[] args) {
        Animal tansty = new Animal(1, "tansty", "🐷");
        Animal tansty1 = new Animal(2, "tansty1", "💩");
        Animal tansty2 = new Animal(3, "tansty2", "🐼");
        SingleLinkList singleLinkList = new SingleLinkList();
        SingleLinkList list = new SingleLinkList();
        singleLinkList.addByOrder(tansty);
        singleLinkList.addByOrder(tansty1);
        list.addByOrder(tansty);
        singleLinkList.combineOrderLink(list);
        singleLinkList.list();
    }
}

class Animal {
    int id;
    String name;
    String nickName;
    Animal next;

    public Animal(int id, String name, String nickName) {
        this.id = id;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "Animal{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}
